﻿#include "mainwindowtest.h"
#include "ui_mainwindowtest.h"
#include <QDebug>
#include <QQmlEngine>
#include <QQmlContext>
#include <QApplication>
#include <QMouseEvent>
#include <QPixmap>
#include <QSettings>
#include <QDir>
#include <QMenu>
#include <QTextCodec>

MainWindowTest::MainWindowTest(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindowTest)
{
    ui->setupUi(this);
    mHook.SetWnd((HWND)window()->winId());//设置钩子所需句柄

    QPixmap pixmap(":/bg.png");
    QPalette* palette = new QPalette();
    //把窗口和widget都刷成黑色
    palette->setBrush(QPalette::Background, QBrush(pixmap));//设置黑色画刷
    window()->setPalette(*palette);
    delete palette;//删除画刷

    setEnabled(true);

    ui->label->setText("<html><head/><body><p><span style=\" color:#f1d23c;\">开始</span></p></body></html>");

    name = "/沙僧日记1.txt";

    QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
}

void MainWindowTest::init()
{
    if(desktopWnd) //设置父窗体为桌面
    {
        SetParent((HWND)winId(), desktopWnd);
    }

    setWindowFlags(Qt::FramelessWindowHint);//无边框
    setWindowFlags(Qt::Dialog);//提升为顶级窗口
    showFullScreen();//showtime！全屏！
    mHook.SetHook();//设置钩子

    QFile file(QDir::currentPath() + name);

    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug()<<"-error-"<<file.fileName();
        return;
    }

    QTextStream stream(&file);

    QTextCodec *codec = QTextCodec::codecForName("GBK");

    stream.setCodec(codec);

    tempData = (stream.readAll());

    file.close();

//    QSettings settings("save.ini");
//    lineNum = int(settings.value("line"));
//    lineNum = 0;
}

MainWindowTest::~MainWindowTest()
{    
    qDebug()<<"~MainWindowTest";
    delete ui;
}

void MainWindowTest::closeEvent(QCloseEvent *event)//重写关闭事件
{
    qDebug()<<"closeEvent";

    event->accept();
}

void MainWindowTest::handle()
{
//    lineNum += 50;
    if(tempData.length()<=0){
        ui->label->setText("<html><head/><body><p><span style=\" color:#f1d23c;\">结束</span></p></body></html>");
        if(desktopWnd) //设置父窗体为桌面
        {
            SetParent((HWND)winId(), desktopWnd);
        }
        return;
    }

    QString d = tempData.left(40);

    ui->label->setText("<html><head/><body><p><span style=\" color:#f1d23c;\">"+d+"</span></p></body></html>");
    tempData = tempData.remove(0, 40);

    if(desktopWnd) //设置父窗体为桌面
    {
        SetParent((HWND)winId(), desktopWnd);
    }
}

void MainWindowTest::saveFile()
{
    QFile file(QDir::currentPath() + name);

    if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        return;
    }

    QTextStream stream(&file);

    QTextCodec *codec = QTextCodec::codecForName("GBK");

    stream.setCodec(codec);

    stream<<tempData;

//    file.write(tempData.toLocal8Bit());


    file.close();

    mHook.UnHook();//卸载钩子
}

bool MainWindowTest::event(QEvent *event)
{
//    qDebug()<<"event"<<event->type();
    if(event->type()==QEvent::KeyPress)
    {
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
        qDebug()<<"event---"<<keyEvent->type()<<keyEvent->text();
//        handle();
        return true;
    }
    else if(event->type()==QEvent::MouseButtonPress)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
//        qDebug()<<"mouseEvent---"<<mouseEvent->type();
        handle();
        return true;
    }
    else if(event->type() == QEvent::Close)
    {
        saveFile();
        return true;
    }
    return false;
}

bool MainWindowTest::nativeEvent(const QByteArray &eventType, void *message, long *result)//获取本地事件
{
    Q_UNUSED(eventType);
    Q_UNUSED(result);

    MSG* msg = static_cast<MSG*>(message);
    if(msg->message==WM_MOUSELEAVE)
        return true;//这里是为了去掉本来不该存在的mouseleave消息（邪术）
    return false;
}
